// rn尺寸缩放
import { Dimensions, PixelRatio, Platform, StyleSheet, } from 'react-native';

export const deviceIsAndroid = Platform.OS === 'android';

export const deviceRatio = PixelRatio.get();

export const deviceWidth = Dimensions.get('window').width;
export const deviceHeight = Dimensions.get('window').height;
export const devicePixWidth = PixelRatio.getPixelSizeForLayoutSize(deviceWidth);
export let devicePixHeight = PixelRatio.getPixelSizeForLayoutSize(deviceHeight);

// 设计稿尺寸计算缩放
export const deviceWindowSize = { width: 750, height: 1334 };

// android 状态栏高度
export const deviceFontScale = deviceRatio / PixelRatio.getFontScale();
let Statu = (deviceRatio < 3.5 ? 4 : deviceRatio);
export const deviceStatusbarHeight = 24.1 * Statu;
if (deviceIsAndroid) {
  devicePixHeight = devicePixHeight - deviceStatusbarHeight;
}

export const deviceScale = (1 / (deviceWindowSize.width / devicePixWidth)) / deviceRatio;
export const deviceWidthScale = (1 / (deviceWindowSize.width / devicePixWidth)) / deviceRatio;
export const deviceHeightScale = (1 / (deviceWindowSize.height / devicePixHeight)) / deviceRatio;

export const System = {
  ratio: deviceRatio,
  width: deviceWidth,
  height: deviceHeight,
  pixWidth: devicePixWidth,
  pixHeight: devicePixHeight,
  scale: deviceScale,
  scaleWidth: deviceWidthScale,
  scaleHeight: deviceHeightScale,
  isAndroid: deviceIsAndroid,
  windowWidth: deviceWindowSize.width,
  windowHeight: deviceWindowSize.height,
  windowStatusbarHeight: deviceStatusbarHeight,
  fontScale: deviceFontScale
};


export function normalize(size) {

  //  size = Math.round((size * System.scale) * deviceRatio);
  //  return size;

  if (deviceRatio === 2) {
    // iphone 5s and older Androids
    if (deviceWidth < 360) {
      return size * 0.95;
    }
    // iphone 5
    if (deviceHeight < 667) {
      return size;
      // iphone 6-6s
    } else if (deviceHeight >= 667 && deviceHeight <= 735) {
      return size * 1.15;
    }
    // older phablets
    return size * 1.25;
  }
  if (deviceRatio === 3) {
    // catch Android font scaling on small machines
    // where pixel ratio / font scale ratio => 3:3
    if (deviceWidth <= 360) {
      return size;
    }
    // Catch other weird android width sizings
    if (deviceHeight < 667) {
      return size * 1.15;
      // catch in-between size Androids and scale font up
      // a tad but not too much
    }
    if (deviceHeight >= 667 && deviceHeight <= 735) {
      return size * 1.2;
    }
    // catch larger devices
    // ie iphone 6s plus / 7 plus / mi note 等等
    return size * 1.27;
  }
  if (deviceRatio === 3.5) {
    // catch Android font scaling on small machines
    // where pixel ratio / font scale ratio => 3:3
    if (deviceWidth <= 360) {
      return size;
      // Catch other smaller android height sizings
    }
    if (deviceHeight < 667) {
      return size * 1.20;
      // catch in-between size Androids and scale font up
      // a tad but not too much
    }
    if (deviceHeight >= 667 && deviceHeight <= 735) {
      return size * 1.25;
    }
    // catch larger phablet devices
    return size * 1.40;
  }
  // if older device ie deviceRatio !== 2 || 3 || 3.5
  return size;
}

const paramStyle = [
  'width',
  'height',
  'top',
  'left',
  'right',
  'bottom',
  'lineHeight',
  'letterSpacing',
  'margin',
  'marginTop',
  'marginRight',
  'marginBottom',
  'marginLeft',
  'marginHorizontal',
  'marginVertical',
  'padding',
  'paddingTop',
  'paddingRight',
  'paddingBottom',
  'paddingLeft',
  'paddingHorizontal',
  'paddingVertical',
  'borderRadius',
  'borderTopLeftRadius',
  'borderTopRightRadius',
  'borderBottomRightRadius',
  'borderBottomLeftRadius',
  'borderWidth',
  'borderTopWidth',
  'borderRightWidth',
  'borderBottomWidth',
  'borderLeftWidth',
  'fontSize'
];

const paramStyleAndroid = [
  'width',
  'top',
  'left',
  'right',
  'bottom',
  'lineHeight',
  'letterSpacing',
  'margin',
  'marginTop',
  'marginRight',
  'marginBottom',
  'marginLeft',
  'marginHorizontal',
  'marginVertical',
  'padding',
  'paddingTop',
  'paddingRight',
  'paddingBottom',
  'paddingLeft',
  'paddingHorizontal',
  'paddingVertical',
  'borderRadius',
  'borderTopLeftRadius',
  'borderTopRightRadius',
  'borderBottomRightRadius',
  'borderBottomLeftRadius',
  'borderWidth',
  'borderTopWidth',
  'borderRightWidth',
  'borderBottomWidth',
  'borderLeftWidth',
  'fontSize'
];

const paramStyleHeight = [
  'height',
];

export function Rem(px) {
  return px * System.scale;
}

export function RemW(px) {
  return px * deviceWidthScale;
}

export function RemH(px) {
  return px * deviceHeightScale;
}

export function createStyles(styles) {
  const platformStyles = {};
  for (let key in styles) {
    let { ios, android, ...style } = { ...styles[key] };
    if (ios && Platform.OS === 'ios') {
      style = { ...style, ...ios };
    }
    if (android && Platform.OS === 'android') {
      style = { ...style, ...android };
    }

    // if (style.fontSize) {
    // style.fontSize = Rem(style.fontSize);
    // style.fontSize = normalize(style.fontSize);
    // }

    if (Platform.OS === 'ios') {
      paramStyle.map((k) => {
        if (style[k]) {
          style[k] = Rem(style[k]);
        }
      });
    }

    if (Platform.OS === 'android') {
      paramStyleAndroid.map((k) => {
        if (style[k]) {
          style[k] = Rem(style[k]);
        }
      });
      paramStyleHeight.map((k) => {
        if (style[k]) {
          style[k] = RemH(style[k]);
        }
      });


    }

    platformStyles[key] = style;
  }
  return StyleSheet.create(platformStyles);

}

